package com.project.quiktask.controller;

import java.sql.Date;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 *  this class provide utilities for date and time handling
 * @author PHAM MINH VIET
 *
 */
public class DateTimeUtil {
	
	public static Time tryParseTime(String input)  {
		Time time = null;
		SimpleDateFormat sdf = new SimpleDateFormat();
		java.util.Date sdfTime;
		try{
		if (input.matches("[0-9]{2}:[0-9]{2}:[0-9]{2}")) {
			sdf = new SimpleDateFormat("HH:mm:ss");
		} else if (input.matches("[0-9]{1,2}:[0-9]{1,2}")) {
			sdf = new SimpleDateFormat("HH:mm");
		} else if (input.matches("[0-9]{1,2}")) {
			sdf = new SimpleDateFormat("HH");
		} else if(input.matches("[0-9]{1,2}(a|p)[m]")){
			input=fillTimeSpecial(input);
			sdf = new SimpleDateFormat("HH");
		}
		else {
			return time;
		}
		sdf.setLenient(false);
		sdfTime = sdf.parse(input);
		time = new Time(sdfTime.getTime());
		}catch(ParseException e){
			time=null;
		}
		return time;
	}
	public static String fillTimeSpecial(String input){
		if(input.contains("pm")){
			int hour=Integer.parseInt(input.substring(0,input.indexOf("p")));
			hour+=12;
			return Integer.toString(hour);
		}
		return input.substring(0, input.indexOf("a"));
	}
	public static Date tryParseDate(String input) {
		Date date = null;
		SimpleDateFormat sdf = new SimpleDateFormat();
		java.util.Date sdfDate;
		input=input.replace('/', '-');
		try{
		if(input.matches("[0-9]{4}(-|/)[0-9]{1,2}(-|/)[0-9]{1,2}")) {
			sdf = new SimpleDateFormat("yyyy-MM-dd");
		}
		else if (input.matches("[0-9]{1,2}(-|/)[0-9]{1,2}(-|/)[0-9]{4}")) {
			sdf = new SimpleDateFormat("dd-MM-yyyy");
		}else if (input.matches("[0-9]{1,2}(-|/)[0-9]{1,2}(-|/)[0-9]{4}")) {
			sdf = new SimpleDateFormat("dd-MM-yyyy");
		} else if (input.matches("[0-9]{1,2}(-|/)[0-9]{1,2}(-|/)[0-9]{2}")) {
			input = fillYearSpecial(input);
			sdf = new SimpleDateFormat("dd-MM-yyyy");
		} else if (input.matches("[0-9]{1,2}(-|/)[0-9]{1,2}")) {
			boolean isNextYear;
			isNextYear = checkIfDateOfNextYear(input);
			input = getFullDate(input, isNextYear);
			sdf = new SimpleDateFormat("dd-MM-yyyy");
		} 
		sdf.setLenient(false);
		sdfDate = sdf.parse(input);
		date = new Date(sdfDate.getTime());
		}catch(ParseException e){
			date=null;
		}
		return date;
	}
	
	public static String fillYearSpecial(String input) {
		assert input.matches("[0-9]{1,2}-[0-9]{1,2}-[0-9]{2}");

		char[] charArray = input.toCharArray();
		char[] tempArray = new char[charArray.length + 2];
		for (int i = 0; i < charArray.length; i++) {
			tempArray[i] = charArray[i];
		}
		tempArray[tempArray.length - 1] = tempArray[tempArray.length - 3];
		tempArray[tempArray.length - 2] = tempArray[tempArray.length - 4];
		tempArray[tempArray.length - 3] = '0';
		tempArray[tempArray.length - 4] = '2';
		String answer = new String(tempArray);

		return answer;
	}

	public static boolean checkIfDateOfNextYear(String input) {
		assert input.matches("[0-9]{1,2}(-)[0-9]{1,2}");

		boolean isNextYear;

		Calendar c = Calendar.getInstance();
		int currentDate = c.get(Calendar.DATE);
		int currentMonth = c.get(Calendar.MONTH) + 1;

		String[] temp = input.split("-");
		if (Integer.parseInt(temp[1]) > currentMonth) {
			isNextYear = false;
		} else if (Integer.parseInt(temp[1]) < currentMonth) {
			isNextYear = true;
		} else {
			if (Integer.parseInt(temp[0]) >= currentDate) {
				isNextYear = false;
			} else {
				isNextYear = true;
			}
		}

		return isNextYear;
	}
	
	public static String getFullDate(String input, boolean isNextYear) {
		assert input.matches("[0-9]{1,2}(-|/)[0-9]{1,2}");

		String answer = input;
		Calendar c = Calendar.getInstance();

		if (isNextYear) {
			answer = answer + "-" + (c.get(Calendar.YEAR) + 1);
		} else {
			answer = answer + "-" + c.get(Calendar.YEAR);
		}
		return answer;
	}

}
